"""
自动选品工作线程
"""
from PyQt6.QtCore import QThread, pyqtSignal
from biz.auto_select_commodity import AutoSelectCommodity

class AutoSelectWorker(QThread):
    """自动选品工作线程"""
    finished = pyqtSignal()
    error = pyqtSignal(str)
    log = pyqtSignal(str)
    progress = pyqtSignal(int, int)  # 当前进度, 总数
    result = pyqtSignal(list)

    def __init__(self, params):
        super().__init__()
        self.params = params
        self.auto_select = AutoSelectCommodity()
        # 连接信号
        self.auto_select.log_signal.connect(self.log.emit)
        self.auto_select.progress_signal.connect(self.progress.emit)
        self.auto_select.result_signal.connect(self.result.emit)

    def run(self):
        """执行选品"""
        try:
            self.auto_select.select_commodities(
                wear_type=self.params['wear_type'],
                min_wear=self.params['min_wear'],
                max_wear=self.params['max_wear'],
                min_price=self.params['min_price'],
                max_price=self.params['max_price'],
                premium_rate=self.params['premium_rate'],
                min_count=self.params['min_count']
            )
            self.finished.emit()
        except Exception as e:
            self.error.emit(str(e))
